﻿using Android.App;
using Android.Content;
using Android.Runtime;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NightPlayer
{
    [Application]
    internal class Program : Android.App.Application
    {

        public static event EventHandler<EventArgs> OnErrorClearCache;

        public static void RequestClearCache()
        {
            OnErrorClearCache?.Invoke(null, null);
        }

        public static AppSettings AppSettings;

        private static ISharedPreferences GetGlobalSharedPreference(Context ctx)
        {
            return ctx.GetSharedPreferences("global_settings",FileCreationMode.Private);
        }

        public void LoadSettings()
        {
            var str = GetGlobalSharedPreference(this).GetString("json", "");
            if (string.IsNullOrWhiteSpace(str))
            {
                AppSettings = new AppSettings();
                SaveSettings(this);
            }
            else
            {
                AppSettings = JsonConvert.DeserializeObject<AppSettings>(str);
            }
        }
        
        public static void SaveSettings(Context ctx)
        {
            GetGlobalSharedPreference(ctx).Edit().PutString("json", JsonConvert.SerializeObject(Program.AppSettings)).Apply();
        }

        public static bool isEink
        {
            get
            {
                return AppSettings.IsEink;
            }
        }

        public Program()
        {
        }

        protected Program(nint javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();
            LoadSettings();

        }

    }
    public class AppSettings
    {
        public bool IsEink = false;
        public bool UseLockscreenMusicSwitch = true;
        public bool SleepTimerAwareness = true;
        public bool PreferLibraryCache = false;
        public bool UDPMusicSwitch = true;
    }

}
